package com.reebake.ideal.message.websocket.core;

import com.reebake.ideal.message.websocket.entity.WebsocketMessageTransferDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.messaging.simp.SimpMessagingTemplate;
 
@Slf4j
public class RedisWebsocketMessageListener extends AbstractWebsocketMessageListener implements MessageListener {
	@Autowired
	private SimpMessagingTemplate messagingTemplate;
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	@Autowired
	private WebsocketServer websocketServer;

	public RedisWebsocketMessageListener(RedisMessageListenerContainer listenerContainer) {
		listenerContainer.addMessageListener(this, new ChannelTopic(WebsocketConstants.QUEUE_NAME_WEBSOCKET_CHANNEL_TOPIC));
	}
 
    @Override
    public void onMessage(Message message, byte[] pattern) {
    	WebsocketMessageTransferDTO entity = (WebsocketMessageTransferDTO) redisTemplate.getValueSerializer().deserialize(message.getBody());;
    	super.handleMessage(entity);
    }
}
